package apis

import (
	"k8sdev/pkg/services"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

type GVR struct {
	Group   string
	Version string
	Name    string
}

func GetGVR(c *gin.Context) {
	var gvrList []GVR
	apiResourceList, err := services.GetGVR()
	if err != nil {
		log.Fatal(err.Error())
		return
	}

	for _, list := range apiResourceList {
		gv, err := schema.ParseGroupVersion(list.GroupVersion)
		if err != nil {
			log.Fatal(err.Error())
			return
		}
		for _, resource := range list.APIResources {
			g := GVR{}
			g.Group = gv.Group
			g.Version = gv.Version
			g.Name = resource.Name
			gvrList = append(gvrList, g)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"GVR": gvrList,
	})
}
